#include <stdio.h>
#include <fcntl.h>
#include <sys/syscall.h>

void error(char*, ...);
/* copy file1 to file2 */
int main(int argc, const char** argv)
{
    int f1, f2, n;
    char buf[BUFSIZ];

    if (argc != 3)
    {
        error("Usage: cp from to");
    }
    if ((f1 = open(argv[1], O_RDONLY, 0)) == -1)
    {
        error("cp: can't open %s", argv[1]);
    }
    if ((f2 = creat(argv[2], 0666)) == -1)
    {
        error("cp: cant't create %s, mode %03o", argv[2], 0666);
    }
    while ((n = read(f1, buf, BUFSIZ)) > 0)
    {
        if (write(f2, buf, n) != n)
        {
            error("cp: write error on file %s", argv[2]);
        }
        return 0;
    }
}
